package com.yuanrui.practice.server.subject;


import com.yuanrui.practice.server.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目类型处理器工厂
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
    //利用spring容器的特性，将所有的处理器注入
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlers;

    //存放所有处理器
    private final Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    //根据题目类型获取对应的处理器
    public SubjectTypeHandler getHandler(int subjectType) {
        return handlerMap.get(SubjectInfoTypeEnum.getByCode(subjectType));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //在bean初始化完成后，将所有的处理器放入map中
        for (SubjectTypeHandler handler : subjectTypeHandlers) {
            handlerMap.put(handler.getSubjectType(), handler);
        }
    }
}
